<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>

<html>
  <head>
   <title>整表查询数据</title>
   <!-- 声明变量：接收查询语句返回的结果 -->
    <%! ResultSet rs; %>
    <%
    	String dbName = "studb";
    	String userName = "root";
    	String userPassword = "123456";
    	String url = "jdbc:mysql://localhost/"+
    		dbName+"?user="+userName
    		+"&password="+userPassword;
    	//1、加载JDBC驱动
    	Class.forName("com.mysql.jdbc.Driver");
    	//2、连接数据库
    	Connection conn =DriverManager.getConnection(url);
    	//3、创建语句对象，用来执行SQL语句
    	Statement s = conn.createStatement();
    	//4、定义SQL语句（查询student表中的所有数据：*代表所有列）
    	String sql = "select * from student";
    	//5、执行sql语句,并将结果存储到rs（ResultSet）变量中
    	rs = s.executeQuery(sql);
    	//6、关闭连接
    	//conn.close();
     %>

	

  </head>
  
  <body>
  <table border="1">
	  <th colspan="4" style="font-size: x-large;alignment: center">学生信息表</th>
	  <tr style="text-align: center">
		  <td>学号</td>
		  <td>姓名</td>
		  <td>出生日期</td>
		  <td>所在班级</td>
	  </tr>
	  <%
		  //循环遍历rs中数据
		  while(rs.next()){
	  %>
	  <tr style="text-align: center">
		  <td><%=rs.getString(1)%></td>
		  <td><%=rs.getString(2)%></td>
		  <td><%=rs.getString("birthday")%></td>
		  <td><%=rs.getString("stuclass")%></td>
	  </tr>
	  <%}%>

  <%
  //关闭原则：先创建的后关闭
  rs.close();
  s.close();
  conn.close();
  %>
  </table>
  </body>
</html>
